<script lang="ts" setup>
import type { StateType } from './types';

import { computed } from 'vue';
import { useRoute } from 'vue-router';

import { Page } from '@vben/common-ui';
import { useTabs } from '@vben/hooks';
import { $t } from '@vben/locales';
import { useTitle } from '@vueuse/core';
import {
  Card,
  message,
  Select,
  SelectOption,
  TabPane,
  Tabs,
} from 'ant-design-vue';
import dayjs from 'dayjs';
import { storeToRefs } from 'pinia';
import { pinyin } from 'pinyin-pro';

import { useVbenForm } from '#/adapter/form';
import { getEmailTemplatInfo, postEmailTemplat, putEmailTemplat } from '#/api/admin/emailTemplate';
import { usePreferencesStore } from '#/store';

const route = useRoute();

const { setTabTitle, closeCurrentTab } = useTabs();

const index: any = computed(() => {
  return route.params?.id ?? -1;
});
const router = useRouter();

const routeMeta = router.currentRoute.value.meta;
const routeTitle = routeMeta.title.replace('管理', '');
const preferencesStore = usePreferencesStore();
const { title } = storeToRefs(preferencesStore);
const pageTitle =
  Number.parseInt(index.value, 10) === 0
    ? `新增${routeTitle}`
    : `No.${index.value} - 编辑${routeTitle}`;
setTabTitle(pageTitle);
useTitle(`${pageTitle} - ${title.value}`);

const disabled = ref(true);
const activeKey = ref('1');
const state: StateType = reactive({
  form: {
    title: '',
    des: '',
    content:''
   
  },
  pageThemeFiles: [],
});

const [BasicForm, BasicFormApi] = useVbenForm({
  handleSubmit: onSubmit,
  resetButtonOptions: {
    show: false,
  },
  schema: [
    {
      component: 'Input',
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: ['id'],
      },
      fieldName: 'id',
      label: 'ID',
    },
    {
      component: 'Input',
      defaultValue: '',
      dependencies: {
        trigger: (values) => {
          disabled.value = !values.title;
          state.form.content = values.content;
          state.form.title = values.title;
          state.form.status = values.status;
          state.form.type = values.type;
        },
        // 随意一个字段改变时，都会触发
        triggerFields: [
          'id',
          'title',
          'status',
          'content',
          'type'
        ],
      },
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'title',
      label: `${routeTitle}标题`,
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        options: [
          {
            label: 'Publish',
            value: 1,
          },
          {
            label: 'Trash',
            value:  0,
          },
          
        ],
      },
      fieldName: 'status',
      rules: 'required', 
      label: '状态',
    },
 
   {
      component: 'Select',
      defaultValue: '',
      componentProps: {
      placeholder: $t('page.common.Please Enter'),
      class:'w-full',
      allowClear: true,
      options:[
            {
                'label':'新车发布',
                'value':'vechile_publich'
            },
            {
                'label':'促销活动',
                'value':'promotion'
            },
            {
                'label':'新闻发布',
                'value':'news_publish'
            },
            {
                'label':'车子程序技术升级',
                'value':'vechile_up'
            },
        ],
      },
      fieldName: 'type',
      label: `${routeTitle}`,
    },
    {
      component: 'Editor',
      defaultValue: '',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'content',
      label: '内容',
    },
  ],
});



async function onSubmit() {
  function showMsg(msg: string) {
    message.success(msg);
    closeCurrentTab();

      router.push({
          name: `Adminemailtemplate`,
     });
  }
  const values: any = state.form;

  if (values.id) {
    const { msg } = await putEmailTemplat(values);
    showMsg(msg);
  } else {
    delete values.id;
    const { msg } = await postEmailTemplat(values);
    showMsg(msg);
  }
}
async function onGetPage(id: number) {
  const { data } = await getEmailTemplatInfo({id:id});
  data.post = {
    id: data?.id ?? undefined,
    title: data?.title ?? '',
    content: data?.content ?? '',
    status: data?.status ?? '',
    type:data?.type??''
  };
  state.form = data.post;
  BasicFormApi.setValues({ ...state.form});
  state.pageThemeFiles = data.pageThemeFiles;
}

onMounted(async () => {
  if(index.value>0){
  
          await onGetPage(index.value);
  }
  
});
</script>

<template>
  <Page>
    <Card :title="`${routeTitle}详情`">
      <BasicForm />
    </Card>
  </Page>
</template>
